package cn.herlioner.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import cn.herlioner.dao.SpecialityDAO;
import cn.herlioner.pojo.TSObject;
import cn.herlioner.pojo.Speciality;

public class SpecialityDAOImpl implements SpecialityDAO{

	@Override
	public void add(Speciality s) {
		SessionFactory sf = new Configuration().configure()
				.buildSessionFactory();
		Session se = sf.openSession();
		se.beginTransaction();
		
		se.save(s);
		
		se.getTransaction().commit();
		se.close();
		sf.close();
		
	}

	@Override
	public Speciality get(int spenum) {
		Speciality result = null;
		 
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
 
        result = (Speciality) s.get(Speciality.class, spenum);
 
        s.close();
        sf.close();
        return result;
	}

	@Override
	public void delete(int spenum) {
		 SessionFactory sf = new Configuration().configure()
	                .buildSessionFactory();
	        Session s = sf.openSession();
	        s.beginTransaction();
	 
	        Speciality p = (Speciality) s.get(Speciality.class, spenum);
	 
	        s.delete(p);
	 
	        s.getTransaction().commit();
	        s.close();
	        sf.close();
		
	}

	@Override
	public void update(Speciality o) {
		SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
        s.beginTransaction();
 
        s.update(o);
 
        s.getTransaction().commit();
        s.close();
        sf.close();
		
	}

	@Override
	public List<Speciality> list() {
		List<Speciality> result = new ArrayList();
		 
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
 
        Query q = s.createQuery("from Speciality s");
 
        result = q.list();
 
        s.close();
        sf.close();
        return result;
	}

}
